package com.sgb.kjwl.view.widget.self.goods;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.SkuDetail;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class SetMemberPricePopWindow extends BasePopWindow {

    private TextView okView;
    private TextView resetView;
    private TextView itemTvAttribute;
    private ItemInputTextView itemTvPrice;
    private ItemInputTextView itemTvMemberPrice;
    private ItemInputTextView itemTvMemberPrice1;
    private ItemInputTextView itemTvMemberPrice2;
    private ItemInputTextView itemTvMemberPrice3;
    private ItemInputTextView itemTvMemberPrice4;
    private ItemInputTextView itemTvMemberPrice5;
    private BigDecimal[] list;
    private BigDecimal maxPrice;
    private BigDecimal promotionPrice;//促销价格
    private BigDecimal parityPrice;//销售价格
    private int promoteStatus;
    private SkuDetail.SkuListEntity skuListEntity;

    public SetMemberPricePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_set_member_price;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        itemTvAttribute = holder.getPop().findViewById(R.id.item_tv_attribute);
        itemTvPrice = holder.getPop().findViewById(R.id.item_tv_price);
        itemTvMemberPrice = holder.getPop().findViewById(R.id.item_tv_member_price);
        itemTvMemberPrice1 = holder.getPop().findViewById(R.id.item_tv_member_price1);
        itemTvMemberPrice2 = holder.getPop().findViewById(R.id.item_tv_member_price2);
        itemTvMemberPrice3 = holder.getPop().findViewById(R.id.item_tv_member_price3);
        itemTvMemberPrice4 = holder.getPop().findViewById(R.id.item_tv_member_price4);
        itemTvMemberPrice5 = holder.getPop().findViewById(R.id.item_tv_member_price5);
        itemTvMemberPrice.setCheck(9, 2);
        itemTvMemberPrice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        itemTvMemberPrice1.setCheck(9, 2);
        itemTvMemberPrice1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        itemTvMemberPrice2.setCheck(9, 2);
        itemTvMemberPrice2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        itemTvMemberPrice3.setCheck(9, 2);
        itemTvMemberPrice3.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        itemTvMemberPrice4.setCheck(9, 2);
        itemTvMemberPrice4.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        itemTvMemberPrice5.setCheck(9, 2);
        itemTvMemberPrice5.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkData(itemTvMemberPrice.getContent(),
                        itemTvMemberPrice1.getContent(),
                        itemTvMemberPrice2.getContent(),
                        itemTvMemberPrice3.getContent(),
                        itemTvMemberPrice4.getContent(),
                        itemTvMemberPrice5.getContent());
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 数据验证
     *
     * @param price  普通会员价
     * @param price1 一星会员价
     * @param price2 二星会员价
     * @param price3 三星会员价
     * @param price4 四星会员价
     * @param price5 五星会员价
     */
    private void checkData(String price, String price1, String price2, String price3, String price4, String price5) {
        if (TextUtils.isEmpty(price) || TextUtils.isEmpty(price1) || TextUtils.isEmpty(price2) || TextUtils.isEmpty(price3) || TextUtils.isEmpty(price4) || TextUtils.isEmpty(price5)) {
            MToast.showToast(mContext, "请输入含税单价");
            return;
        }
        getMaxPrice(price, price1, price2, price3, price4, price5);
    }

    /**
     * 获取最大价格
     *
     * @param price  普通会员价
     * @param price1 一星会员价
     * @param price2 二星会员价
     * @param price3 三星会员价
     * @param price4 四星会员价
     * @param price5 五星会员价
     */
    private void getMaxPrice(String price, String price1, String price2, String price3, String price4, String price5) {
        BigDecimal bd = new BigDecimal(price);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd1 = new BigDecimal(price1);
        bd1 = bd1.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd2 = new BigDecimal(price2);
        bd2 = bd2.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd3 = new BigDecimal(price3);
        bd3 = bd3.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd4 = new BigDecimal(price4);
        bd4 = bd4.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd5 = new BigDecimal(price5);
        bd5 = bd5.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal ZERO = new BigDecimal(0.00);
        ZERO = ZERO.setScale(2, BigDecimal.ROUND_HALF_UP);
        if (bd.compareTo(ZERO) == 0 || bd1.compareTo(ZERO) == 0 || bd2.compareTo(ZERO) == 0 || bd3.compareTo(ZERO) == 0 || bd4.compareTo(ZERO) == 0 || bd5.compareTo(ZERO) == 0) {
            MToast.showToast(mContext, "会员价不能小于等于0");
            return;
        }
        list = new BigDecimal[]{bd, bd1, bd2, bd3, bd4, bd5};
        bubbleSort(list);
        setPrice(bd, bd1, bd2, bd3, bd4, bd5);
        maxPrice = list[list.length - 1];
        if (maxPrice.compareTo(parityPrice) == 1) {
            MToast.showToast(mContext, "会员价不能高于销售价");
            return;
        } else if (promoteStatus == 2 && maxPrice.compareTo(promotionPrice) == -1) {
            MToast.showToast(mContext, "会员价不能低于促销价");
            return;
        }
        skuListEntity.setPrice(bd + "");
        skuListEntity.setPrice1(bd1 + "");
        skuListEntity.setPrice2(bd2 + "");
        skuListEntity.setPrice3(bd3 + "");
        skuListEntity.setPrice4(bd4 + "");
        skuListEntity.setPrice5(bd5 + "");
        skuListEntity.setSet(true);
        if (onResultClick != null) {
            onResultClick.result(skuListEntity, null, null);
        }
        dismiss();
    }

    /**
     * 获取最大价格
     *
     * @param price  普通会员价
     * @param price1 一星会员价
     * @param price2 二星会员价
     * @param price3 三星会员价
     * @param price4 四星会员价
     * @param price5 五星会员价
     */
    private void setNullPrice(String price, String price1, String price2, String price3, String price4, String price5) {
        BigDecimal bd = new BigDecimal(price);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd1 = new BigDecimal(price1);
        bd1 = bd1.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd2 = new BigDecimal(price2);
        bd2 = bd2.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd3 = new BigDecimal(price3);
        bd3 = bd3.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd4 = new BigDecimal(price4);
        bd4 = bd4.setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal bd5 = new BigDecimal(price5);
        bd5 = bd5.setScale(2, BigDecimal.ROUND_HALF_UP);
        list = new BigDecimal[]{bd, bd1, bd2, bd3, bd4, bd5};
        for (int i = 0; i < list.length; i++) {
            if (list[i].compareTo(parityPrice) == 1) {
                forPrice(i);
            } else if (promoteStatus == 2 && maxPrice.compareTo(promotionPrice) == -1) {
                forPrice(i);
            }
        }
        skuListEntity.setChange(false);
    }

    private void forPrice(int i) {
        switch (i) {
            case 0:
                skuListEntity.setPrice("");
                itemTvMemberPrice.setContent("");
                break;
            case 1:
                itemTvMemberPrice1.setContent("");
                skuListEntity.setPrice1("");
                break;
            case 2:
                itemTvMemberPrice2.setContent("");
                skuListEntity.setPrice2("");
                break;
            case 3:
                itemTvMemberPrice3.setContent("");
                skuListEntity.setPrice3("");
                break;
            case 4:
                itemTvMemberPrice4.setContent("");
                skuListEntity.setPrice4("");
                break;
            case 5:
                itemTvMemberPrice5.setContent("");
                skuListEntity.setPrice5("");
                break;
        }
    }

    /**
     * 取最大价格
     *
     * @param list
     */
    private void bubbleSort(BigDecimal[] list) {
        for (int i = list.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (list[j].compareTo(list[j + 1]) == 1) {
                    BigDecimal temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
    }

    /**
     * 设置小数点
     *
     * @param price  普通会员价
     * @param price1 一星会员价
     * @param price2 二星会员价
     * @param price3 三星会员价
     * @param price4 四星会员价
     * @param price5 五星会员价
     */
    private void setPrice(BigDecimal price, BigDecimal price1, BigDecimal price2, BigDecimal price3, BigDecimal price4, BigDecimal price5) {
        itemTvMemberPrice.setContent(price + "");
        itemTvMemberPrice1.setContent(price1 + "");
        itemTvMemberPrice2.setContent(price2 + "");
        itemTvMemberPrice3.setContent(price3 + "");
        itemTvMemberPrice4.setContent(price4 + "");
        itemTvMemberPrice5.setContent(price5 + "");
    }

    public void setData(SkuDetail.SkuListEntity skuListEntity) {
        this.skuListEntity = skuListEntity;
        itemTvAttribute.setText(skuListEntity.getAttr());
        promoteStatus = skuListEntity.getPromoteStatus();
        if (promoteStatus == 2) {
            promotionPrice = new BigDecimal(skuListEntity.getPromotePrice());//促销价
            promotionPrice = promotionPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        parityPrice = new BigDecimal(skuListEntity.getMoney());//销售价
        parityPrice = parityPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
        if (skuListEntity.getPromoteStatus() != 2) {
            itemTvPrice.setContent(skuListEntity.getMoney());
            itemTvPrice.setLabelName("销售价");
        } else {
            itemTvPrice.setContent(skuListEntity.getMoney() + "/" + skuListEntity.getPromotePrice());
            itemTvPrice.setLabelName("销售价/促销价");
        }
        if (skuListEntity.isChange() == false) {
            itemTvMemberPrice.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice()) ? skuListEntity.getPrice() : "");
            itemTvMemberPrice1.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice1()) ? skuListEntity.getPrice1() : "");
            itemTvMemberPrice2.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice2()) ? skuListEntity.getPrice2() : "");
            itemTvMemberPrice3.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice3()) ? skuListEntity.getPrice3() : "");
            itemTvMemberPrice4.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice4()) ? skuListEntity.getPrice4() : "");
            itemTvMemberPrice5.setContent(skuListEntity.isSet() == true || !TextUtils.isEmpty(skuListEntity.getPrice5()) ? skuListEntity.getPrice5() : "");
        } else {
            setNullPrice(skuListEntity.getPrice(), skuListEntity.getPrice1(), skuListEntity.getPrice2(), skuListEntity.getPrice3(), skuListEntity.getPrice4(), skuListEntity.getPrice5());
        }
    }
}
